package com.neuedu.jxcboot.util;

import java.util.Base64;

/**
 * 对称加密和解密工具
 * 
 * @author chens
 *
 */
public class Base64Util {

	/**
	 * 对字符串rawString进行BASE64加密
	 * 
	 * @param rawString
	 * @return
	 */
	public static String encodingString(String rawString) {
		if (rawString == null)
			return null;

		Base64.Encoder encoder = Base64.getEncoder();

		return encoder.encodeToString(rawString.getBytes());
	}

	/**
	 * 解码 BASE64 编码的字符串 encryptedString
	 * 
	 * @param encryptedString
	 * @return
	 */
	public static String decoodingString(String encryptedString) {
		if (encryptedString == null)
			return null;
		Base64.Decoder decoder = Base64.getDecoder();
		try {
			byte[] b = decoder.decode(encryptedString);
			return new String(b);
		} catch (Exception e) {
			return null;
		}
	}

	/**
	 * 对字符串进⾏三次的BASE64编码
	 * 
	 * @param rawString
	 * @return
	 */
	public static String encryption(String rawString) {
		return encodingString(encodingString(encodingString(rawString)));
	}

	/**
	 * 对字符串进⾏三次的BASE64解码
	 * 
	 * @param encryptedString
	 * @return
	 */
	public static String decryption(String encryptedString) {
		return decoodingString(decoodingString(decoodingString(encryptedString)));
	}

	public static void main(String[] args) {
		String a = encryption("100000.89".toString());
		System.out.println(a);// 加密
		System.out.println(decryption(a));// 解密
	}

}
